package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.xuanzheng.company.entity.SystemApplication;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.CompanyService;
import com.xuanzheng.company.service.SystemApplicationService;
import com.xuanzheng.company.service.impl.CompanyServiceImpl;
import com.xuanzheng.company.service.impl.SystemApplicationServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/checkApplicationStatus")
public class CheckApplicationStatusServlet extends HttpServlet {
    private final SystemApplicationService applicationService = new SystemApplicationServiceImpl();
    private final CompanyService companyService = new CompanyServiceImpl();
    private final Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        Map<String, Object> result = new HashMap<>();
        
        // 检查用户会话
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("user") == null) {
            result.put("success", false);
            result.put("message", "请先登录");
            out.write(gson.toJson(result));
            return;
        }
        
        // 获取当前用户
        User user = (User) session.getAttribute("user");
        
        // 仅公司负责人需要检查申请状态
        if (user.getRole() != 1) {
            result.put("success", false);
            result.put("message", "只有公司负责人需要系统使用申请");
            out.write(gson.toJson(result));
            return;
        }
        
        // 获取用户最新的申请
        SystemApplication application = applicationService.getUserLatestApplication(user.getId());
        int status = -1; // 默认未申请
        
        if (application != null) {
            status = application.getStatus();
            result.put("application", application);
            
            // 如果申请已通过，获取并添加公司ID
            if (status == 1) {
                String companyId = user.getCompanyId();
                if (companyId == null) {
                    // 如果用户对象中没有公司ID，尝试从数据库获取
                    companyId = companyService.getCompanyByManagerId(user.getId()) != null ? 
                               companyService.getCompanyByManagerId(user.getId()).getId() : null;
                }
                result.put("companyId", companyId);
            }
        }
        
        result.put("success", true);
        result.put("status", status);
        result.put("statusText", getStatusText(status));
        result.put("canUseSystem", status == 1); // 只有状态为1（已通过）才能使用系统
        
        out.write(gson.toJson(result));
    }
    
    // 获取状态文本描述
    private String getStatusText(int status) {
        switch (status) {
            case -1:
                return "未申请";
            case 0:
                return "审核中";
            case 1:
                return "已通过";
            case 2:
                return "已拒绝";
            default:
                return "未知状态";
        }
    }
} 